package kits

import (
	"strings"
	"time"
)

type dateTimeTool struct{}

var DateTime dateTimeTool

// FormatDateTime 格式化时间为 2006-01-02 15:04:05 格式
func (ft *dateTimeTool) FormatDateTime(dt time.Time) string {
	return dt.Format("2006-01-02 15:04:05")
}

// FormatDate 格式化时间为 2006-01-02 格式
func (ft *dateTimeTool) FormatDate(dt time.Time) string {
	return dt.Format("2006-01-02")
}

// FormatTime 格式化时间为 15:04:05 格式
func (ft *dateTimeTool) FormatTime(dt time.Time) string {
	return dt.Format("15:04:05")
}

// FormatPattern 格式化时间，指定格式，yyyy:yy/MM:M/dd:d/HH:hh:h/mm:m/ss:s/SSS
func (ft *dateTimeTool) FormatPattern(dt time.Time, pattern string) string {
	pattern = strings.ReplaceAll(pattern, "yyyy", "2006")
	pattern = strings.ReplaceAll(pattern, "yy", "06")
	pattern = strings.ReplaceAll(pattern, "MM", "01")
	pattern = strings.ReplaceAll(pattern, "M", "1")
	pattern = strings.ReplaceAll(pattern, "dd", "02")
	pattern = strings.ReplaceAll(pattern, "d", "2")
	pattern = strings.ReplaceAll(pattern, "HH", "15")
	pattern = strings.ReplaceAll(pattern, "hh", "03")
	pattern = strings.ReplaceAll(pattern, "h", "3")
	pattern = strings.ReplaceAll(pattern, "mm", "04")
	pattern = strings.ReplaceAll(pattern, "m", "4")
	pattern = strings.ReplaceAll(pattern, "SSSSSS", "000000")
	pattern = strings.ReplaceAll(pattern, "SSS", "000")
	pattern = strings.ReplaceAll(pattern, "ss", "05")
	pattern = strings.ReplaceAll(pattern, "s", "05")
	return dt.Format(pattern)
}
